<template>
  <div class="app-container calendar-list-container">
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="数据加载中，请稍后" border fit highlight-current-row @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column  v-if='showAllDel' type="selection" width="55">
      </el-table-column>
      
      <el-table-column min-width="240" align="center" label="推送标题" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200" align="center" label="推送平台" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.sdk + '-' + scope.row.platform}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200" align="center" label="消息ID" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.taskId}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="120" align="center" label="过期时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.expireTime | dateformat('YYYY-MM-DD HH:mm') }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="120" align="center" label="创建时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createdAt | dateformat('YYYY-MM-DD HH:mm') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="推送地址" min-width="150">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.skip +' '+scope.row.skipRemarks}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="推送内容" min-width="195">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.content}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="推送状态" min-width="120">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.status | statusNameFilter}}</span>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNo"
        :page-sizes="[10,20,30,50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { getList } from '@/api/push'
export default {
  name: 'table_demo',
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      dateformat:'',
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10
      },
      temp: {
        section:{}
      },
      dialogFormVisible: false,
      rules: {
        title: [{ required: true, message: '标题必填', trigger: 'blur' }],
        'section.name': [{ required: true, message: '所属栏目必填', trigger: 'blur' }],
        'section.tab': [{ required: true, message: '标签必填', trigger: 'blur' }],
        inTime: [{ type: 'date', required: true, message: '发布日期必填', trigger: 'change' }],
        view: [{ required: true, message: '阅读数必填', trigger: 'blur' }],
        platform: [{ required: true, message: '平台必填', trigger: 'blur' }]
      },
      tableKey: 0,
      showAllDel:false,
      multipleSelection:[],
      pageBoolean:{
        delColor:'primary',
        delText:'批量删除'
      }
    }
  },
  mounted() {
    this.getList()
  },
  watch:{
    showAllDel(val){
      if(val){
        this.pageBoolean.delText = '选择删除'
        this.pageBoolean.delColor = 'danger'
      }else{
        this.pageBoolean.delText = '批量删除'
        this.pageBoolean.delColor = 'primary'
      }
    },
    multipleSelection(val){
      if(val.length == 0){
        this.pageBoolean.delText = '选择删除'
        this.pageBoolean.delColor = 'danger'
      }else{
        this.pageBoolean.delText = '确定删除'
        this.pageBoolean.delColor = 'success'
      }
    }
  },
  filters: {
    statusNameFilter(status) {
      const statusMap = {
        '1':'推送成功',
        '0':'推送失败'
      }
      return statusMap[status]
    }
  },
  methods: {
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    handleAllDelete(){
      const { showAllDel, multipleSelection } = this
      if(multipleSelection.length != 0){
        const ids = []
        multipleSelection.map(e => ids.push(e.id))
        const params = { ids, operateType:'DELETE' }
        delIds(params).then(response => {
          const { code } = response
          if(code == 0){
            this.$notify({
              title: '成功',
              message: '批量删除资讯成功',
              type: 'success',
              duration: 2000
            })
            this.handleRefresh()
          }else{
            this.$notify({
              title: '失败',
              message: '批量删除资讯失败',
              type: 'error',
              duration: 2000
            })
          }
        })
      }
      this.showAllDel = !showAllDel
    },
    getList() {
      this.listLoading = true
      const params = this.listQuery
      getList(params).then(response => {
        const { code, data:{ total, list } } = response
        if(code == 0){
          this.total = total
          this.list = list
          this.listLoading = false
        }
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.getList()
    },
    handleModifyStatus(row) {
      row.inTime = new Date(row.inTime)
      this.temp = row
      this.dialogFormVisible = true
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const params = this.temp
          update(params).then(response => {
            const { code } = response
            if(code == 0){
              for (const v of this.list) {
                if (v.id === this.temp.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDelete(idx, row) {
      const { id } = row
      delId(id).then(response => {
        const { code } = response
        if(code == 0){
          this.$notify({
            title: '成功',
            message: '删除资讯成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(idx)
          this.list.splice(idx, 1)
        }else{
          this.$notify({
            title: '失败',
            message: '删除资讯失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    handleRefresh() {
      this.list = null
      this.total = null
      this.listQuery.pageNo = 1
      this.getList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  padding:20px;
}
.pagination-container{
  margin-top:30px;
}
</style>